package weaver.splittable;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import weaver.conn.ConnectionPool;
import weaver.general.BaseBean;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 分表组件配置
 * author: YUEYANG 2023-01-12
 */
public class STConfig {

    public static final String LOGGER_NAME = "splittable";

    private static final Logger LOGGER = Logger.getLogger(LOGGER_NAME);

    // 分表配置文件
    private static final String CONFIG_FILE_NAME = "weaver_split_table";

    // 是否开启分表功能
    // private static boolean ENABLED = false;

    // 默认全量表接口集合
    public static Set<String> FULL_DATA_API_SET = new HashSet<>();

    // 默认活跃表接口集合
    public static Set<String> FAST_DATA_API_SET = new HashSet<>();

    // 分表年限，不配置默认3年
    public static String SPLIT_TIME = "2";

    // 同步数据间隔 单位秒
    public static String SYNC_TIME = "5";

    // 数据变动记录表和数据清理记录表 默认只保留30天的数据
    public static String KEEP_SPLIT_TABLE_TIME = "30";

    public static String DB_TYPE = "oracle";

    // 如果SQL中包含 "BREAK_SPLIT_TABLE_SQL" 则不进行替换
    public static String FAST_DATA_SIGN = "SPLIT_FAST_DATA_SQL";

    // 如果SQL中包含 "FULL_DATA_SIGN" 则直接查询全量表
    public static String FULL_DATA_SIGN = "SPLIT_FULL_DATA_SQL";

    // 存储过程替换配置
    public static Map<String, String> PROC_REPLACE_MAP = new HashMap<>();

    // 默认全量表接口集合
    public static Set<String> BIZ_INITIALIZER_SET = new HashSet<>();

    // 是否开启拦截日志
    public static boolean IS_LOG = false;

    static {
        BaseBean baseBean = new BaseBean();

        // 初始化配置
//        String enabled = baseBean.getPropValue(CONFIG_FILE_NAME,"enabled");
//        if (StringUtils.isNotBlank(enabled) && enabled.equals("1")) {
//            ENABLED = true;
//        } else {
//            ENABLED = false;
//        }
//        LOGGER.info("分表开关：" + ENABLED);

        String splitTime = baseBean.getPropValue(CONFIG_FILE_NAME,"split_time");
        if (StringUtils.isNotBlank(splitTime)) {
            SPLIT_TIME = splitTime;
        }

        String syncTime = baseBean.getPropValue(CONFIG_FILE_NAME,"sync_time");
        if (StringUtils.isNotBlank(syncTime)) {
            SYNC_TIME = syncTime;
        }

        String keepSplitTableTime = baseBean.getPropValue(CONFIG_FILE_NAME,"keep_split_table_time");
        if (StringUtils.isNotBlank(keepSplitTableTime)) {
            KEEP_SPLIT_TABLE_TIME = keepSplitTableTime;
        }

        String defaultFullDataApis = baseBean.getPropValue(CONFIG_FILE_NAME,"full_data_apis");
        if(StringUtils.isNotBlank(defaultFullDataApis)){
            String[] urlArr = defaultFullDataApis.split(",");
            for(String url : urlArr){
                FULL_DATA_API_SET.add(url.toLowerCase().trim());
            }
        }

        String defaultFastDataApis = baseBean.getPropValue(CONFIG_FILE_NAME,"fast_data_apis");
        if(StringUtils.isNotBlank(defaultFastDataApis)){
            String[] urlArr = defaultFastDataApis.split(",");
            for(String url : urlArr){
                FAST_DATA_API_SET.add(url.toLowerCase().trim());
            }
        }

        String procReplaceStr = baseBean.getPropValue(CONFIG_FILE_NAME,"proc_replace");
        if(StringUtils.isNotBlank(procReplaceStr)){
            String[] procReplacesArr = procReplaceStr.split(",");
            for(String procReplaces : procReplacesArr){
                String [] procReplaceArr = procReplaces.split(":");
                if (procReplaceArr.length == 2) {
                    PROC_REPLACE_MAP.put(procReplaceArr[0], procReplaceArr[1]);
                }
            }
        }

        String bizInitializers = baseBean.getPropValue(CONFIG_FILE_NAME,"biz_initializers");
        if(StringUtils.isNotBlank(bizInitializers)){
            String[] bizInitializerArray = bizInitializers.split(",");
            for(String bizInitializer : bizInitializerArray){
                BIZ_INITIALIZER_SET.add(bizInitializer.trim());
            }
        }

        String isLog = baseBean.getPropValue(CONFIG_FILE_NAME,"is_log");
        if (StringUtils.isNotBlank(isLog) && isLog.equals("1")) {
            IS_LOG = true;
        }

        // 初始化DB_TYPE
        DB_TYPE = ConnectionPool.getInstance().getDbtype();

        LOGGER.info("数据库类型：" + DB_TYPE);
        LOGGER.info("分表年限：" + SPLIT_TIME + " 天");
        LOGGER.info("同步间隔：" + SYNC_TIME + " 秒");
        LOGGER.info("中间表数据保留天数：" + KEEP_SPLIT_TABLE_TIME + " 天");
        LOGGER.info("是否开启拦截日志：" + IS_LOG);
        LOGGER.info("默认全量表接口：");
        for (String fullDataApi: FULL_DATA_API_SET) {
            LOGGER.info(fullDataApi);
        }
        LOGGER.info("默认活跃表接口：");
        for (String fastDataApi: FAST_DATA_API_SET) {
            LOGGER.info(fastDataApi);
        }
        LOGGER.info("要替换的存储过程：");
        for (Map.Entry<String, String> procReplaceEntry: PROC_REPLACE_MAP.entrySet()) {
            LOGGER.info(procReplaceEntry.getKey() + ":" + procReplaceEntry.getValue());
        }
        LOGGER.info("业务初始化器：");
        for (String bizInitializer: BIZ_INITIALIZER_SET) {
            LOGGER.info(bizInitializer);
        }
        LOGGER.info("==========");
    }

}
